package com.example.qundui.learndemo.netutils;

import android.os.Environment;

import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.download.DownloadListener;
import com.yanzhenjie.nohttp.download.DownloadQueue;
import com.yanzhenjie.nohttp.download.DownloadRequest;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Map;

import javax.net.ssl.SSLContext;

/**
 * Created by qundui on 2017/4/12.
 */
public class Netutils {
     RequestQueue requestQueue;
     private DownloadRequest downloadRequest;

     public Netutils() {
          requestQueue = NoHttp.newRequestQueue();
     }
  DownloadListener downloadListener = new DownloadListener() {
        @Override
        public void onDownloadError(int what, Exception exception) {

        }
        @Override
          public void onStart(int what, boolean resume, long preLenght, Headers header, long count) {
               // 下载开始
          }

          @Override
          public void onProgress(int what, int progress, long downCount, long speed) {
               // 更新下载进度和下载网速
          }

          @Override
          public void onFinish(int what, String filePath) {
               // 下载完成
          }

          @Override
          public void onCancel(int what) {
               // 下载被取消或者暂停
          }
     };
     Request<String> request;
     MyResponseListners listners=new MyResponseListners();
     INet i;
     public void setI(INet i) {
          this.i = i;
     }
     class  MyResponseListners implements OnResponseListener<String>{

          @Override
          public void onStart(int what) {

          }
          @Override
          public void onSucceed(int what, Response<String> response) {
               if (i!=null){
                    i.onSucceed(what,response);
               }
          }

          @Override
          public void onFailed(int what, Response<String> response) {
               if (i!=null){
                    i.onFailed(what,response);
               }
          }

          @Override
          public void onFinish(int what) {

          }
     }
     public void DoGet(Map<String, String> map, String url, final INet i){

// 如果要指定并发值，传入数字即可：NoHttp.newRequestQueue(3);
           request = NoHttp.createStringRequest(url, RequestMethod.GET);
         SSLContext sslContext = SSLContextUtil.getDefaultSLLContext();
         if (sslContext != null){
             request.setSSLSocketFactory(sslContext.getSocketFactory());
             request.setHostnameVerifier(SSLContextUtil.HOSTNAME_VERIFIER);
         }
          if(map!=null){
               getParam(map);
          }
          requestQueue.add(0, request, listners);

// 发起请求


     }
     public void DoPost(Map<String, String> map, String url, final INet i){
          //requestQueue = NoHttp.newRequestQueue();
// 如果要指定并发值，传入数字即可：NoHttp.newRequestQueue(3);
          request = NoHttp.createStringRequest(url, RequestMethod.POST);
          if(map!=null){
               getParam(map);
          }
          requestQueue.add(0, request, listners);

// 发起请求


     }
     public  void ResuestJosnObj(String url){
          // JsonObject
          Request<JSONObject> objRequest = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
          requestQueue.add(0, objRequest, new OnResponseListener<JSONObject>() {
               @Override
               public void onStart(int what) {

               }

               @Override
               public void onSucceed(int what, Response<JSONObject> response) {

               }

               @Override
               public void onFailed(int what, Response<JSONObject> response) {

               }

               @Override
               public void onFinish(int what) {

               }
          });

     }
     public  void ResuestJosnArray(String url){
// JsonArray
          Request<JSONArray> arrayRequest = NoHttp.createJsonArrayRequest(url, RequestMethod.PUT);
          requestQueue.add(0, arrayRequest, new OnResponseListener<JSONArray>() {
               @Override
               public void onStart(int what) {

               }
               @Override
               public void onSucceed(int what, Response<JSONArray> response) {

               }

               @Override
               public void onFailed(int what, Response<JSONArray> response) {

               }

               @Override
               public void onFinish(int what) {

               }
          });
     }
     public void downloadFile(String url){
          DownloadQueue downloadQueue=NoHttp.newDownloadQueue();
          //下载文件
          String Path= Environment.getExternalStorageDirectory().getPath()+"/app";
          downloadRequest = NoHttp.createDownloadRequest(url,Path,"demo.apk",true,true);
// what 区分下载
// downloadRequest 下载请求对象
// downloadListener 下载监听
          downloadQueue.add(0, downloadRequest, downloadListener);
     }
     private void getParam(Map<String, String> map) {
            request.add(map);

     }

}
